package com.orangecy.rulesubscribe.start;

import com.orangecy.common.exception.OrangeCyException;
import com.orangecy.common.result.PlainResult;
import com.orangecy.rulesubscribe.RuleEngineResult;
import com.orangecy.rulesubscribe.callback.GeneralActionCallBack;
import com.orangecy.rulesubscribe.context.RuleContext;
import com.orangecy.rulesubscribe.template.RuleEngineTemplate;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * @Author : orangeCy.奕
 * @Product : 猎豹养成产品源码
 * @Time : 2021/10/22
 * @Desc : 编排抽象对接入口
 */
public class RuleEngineExecutor {

    @Resource
    private RuleEngineTemplate ruleEngineTemplate;

    private PlainResult<RuleEngineResult> innerExecute(RuleContext c, Supplier<GeneralActionCallBack> s) {
        return ruleEngineTemplate.invoke(c,s.get());
    }

    /**
     * 执行
     * @param c
     * @param s
     * @return
     */
    private RuleEngineResult execute(RuleContext c, Supplier<GeneralActionCallBack> s) {
        //这里可能要追加记录日志 待实现
        PlainResult<RuleEngineResult> execute = innerExecute(c, s);
        if(!execute.isSuccess() || Objects.isNull(execute.getData())) {
            throw new OrangeCyException("抽象编排执行失败");
        }
        return execute.getData();
    }
}